package org.yaac.server.egql.evaluator;

import java.util.Set;

import org.yaac.server.egql.EntityProperty;
import org.yaac.server.egql.exception.EGQLException;
import org.yaac.server.egql.processor.ProcessData.ProcessDataRecord;
import org.yaac.shared.ErrorCode;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class NotEvaluator extends Evaluator {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private final Evaluator op1;

	/**
	 * @param op1
	 */
	public NotEvaluator(Evaluator op1) {
		this.op1 = op1;
	}

	@Override
	public EvaluationResult evaluate(ProcessDataRecord record) {
		EvaluationResult r1 = op1.evaluate(record);
		
		if (r1.getPayload() instanceof Boolean) {
			return new EvaluationResult(!(Boolean)(r1.getPayload()), r1).withTitle(this.getText()); 
		} else {
			return r1.withWarning(ErrorCode.W051);
		}
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((op1 == null) ? 0 : op1.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		NotEvaluator other = (NotEvaluator) obj;
		if (op1 == null) {
			if (other.op1 != null)
				return false;
		} else if (!op1.equals(other.op1))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "NotEvaluator [op1=" + op1 + "]";
	}

	@Override
	public Set<AggregationEvaluator> aggregationChildren() {
		return op1.aggregationChildren();
	}
	
	@Override
	public Set<EntityProperty> nonAggregationProperties() {
		return op1.nonAggregationProperties();
	}

	@Override
	public void validate() throws EGQLException {
		
	}
}
